package com.example.replacebooks.controller;


import com.example.replacebooks.common.Result;
import com.example.replacebooks.entity.Bookinfo;
import com.example.replacebooks.entity.Bookorder;
import com.example.replacebooks.entity.Orders_Bookorder;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.service.BookinfoService;
import com.example.replacebooks.service.BookorderService;
import com.example.replacebooks.service.Orders_BookorderService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单信息 前端控制器
 * </p>
 *
 * @author replacebooks
 * @since 2025-03-03
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/bookorder")
public class BookorderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private BookorderService bookorderService;

    @Resource
    private BookinfoService bookinfoService;

    @Resource
    private Orders_BookorderService orders_bookorderService;

    @GetMapping
    public Result findAll(){return Result.success(bookorderService.findAll());}

    @GetMapping("/getBookinfoById/{bookorderid}")
    public Result getBookinfoById(@PathVariable Integer bookorderid){
        List<Orders_Bookorder> bookorder1 = orders_bookorderService.findByBookorderid(bookorderid);
        ArrayList<Bookinfo> bookinfoList = new ArrayList<>();
        for (Orders_Bookorder bokorder2: bookorder1){
            Integer bookorderid1 = bokorder2.getBookid();
            Bookinfo bookinfo = bookinfoService.findByBookid(bookorderid1);
            bookinfoList.add(bookinfo);
        }
        return Result.success(bookinfoList);
    }

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Bookorder> info = bookorderService.findBySearch(params);
        return Result.success(info);
    }

    @GetMapping("/statusearch")
    public Result findByStatuSearch(Params params){
        PageInfo<Bookorder> info = bookorderService.findByStatuSearch(params);
        return Result.success(info);
    }

    @PostMapping("/pay")
    public Result Pay(@RequestBody Bookorder bookorder){
        bookorderService.pay(bookorder);
        return Result.success();
    }

    @PostMapping("/service")
    public Result Services(@RequestBody Bookorder bookorder){
        bookorderService.service(bookorder);
        return Result.success();
    }

    @PostMapping("/handle")
    public Result Handle(@RequestBody Bookorder bookorder){
        bookorderService.handle(bookorder);
        return Result.success();
    }

    @PostMapping("/add")
    public Result add(@RequestBody Bookorder bookorder){
        bookorderService.add(bookorder);
        return Result.success();
    }


    @DeleteMapping("/{bookorderid}")
    public Result delete(@PathVariable Integer bookorderid){
        bookorderService.delete(bookorderid);
        return Result.success();
    }
}
